<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("jsval/formvalidator.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Task_Template extends Page
{
       public $hiddenTempId = null;
       public $imgBack = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $Image3 = null;
       public $cmbTemplateType = null;
       public $edDescription = null;
       public $memoComments = null;
       public $lblComments = null;
       public $lblDescription = null;
       public $lblTemplateType = null;
       public $edTemplateNo = null;
       public $lblTemplateNo = null;
       function unique_id($id)
       {
        $count = 0;

        do
        {
        $query_check = "SELECT template_num FROM template_tbl WHERE template_num='".$id."'";
        $result_check = mysql_query($query_check) or die("QUERY CHECK ".mysql_error());
        $count = mysql_num_rows($result_check);
        if($count==1 || $id==0)
        $id+=1;
        }while($count==1);

        return $id;
       }
       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }


       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function btnCancelClick($sender, $params)
       {
       redirect('template_list.php?template_id='.$this->hiddenTempId->getValue());


       }

       function Task_TemplateCreate($sender, $params)
       {
       $this->edTemplateNo->Text='';
       $this->cmbTemplateType->Clear();
       $this->edDescription->Text='';
       $this->memoComments->Lines=array();

       $this->imgBack->setLink('template_list.php?template_id='.$this->hiddenTempId->getValue());
       // displays or adds unused style on the combo-box
       $this->cmbTemplateType->Clear();
       $result_style = mysql_query("select * from lookup_vehicle_tbl where company_id='".$_SESSION['company_id']."' and category='body styles'");

       $query_all = "SELECT style_id FROM template_tbl WHERE style_id='All' AND company_id='".$_SESSION['company_id']."'";
       $result_all = mysql_query($query_all) or die("QUERY ALL ".mysql_error());
       $row_all = mysql_num_rows($result_all);

       if($row_all==0)
       $this->cmbTemplateType->AddItem('All', null, 'All');

       while($row_style = mysql_fetch_array($result_style))
        {
            $result_check = mysql_query("SELECT * FROM lookup_vehicle_tbl
            INNER JOIN template_tbl
            ON lookup_vehicle_tbl.value=template_tbl.template_type
            WHERE lookup_vehicle_tbl.value='".$row_style['value']."'
            AND lookup_vehicle_tbl.category='body styles'
            AND lookup_vehicle_tbl.company_id='".$_SESSION['company_id']."'
            AND template_tbl.company_id='".$_SESSION['company_id']."'");
            $row_check = mysql_num_rows($result_check);
            if($row_check==0)
            $this->cmbTemplateType->AddItem($row_style['value'], null, $row_style['id']);


        }

       }

       function btnSubmitClick($sender, $params)
       {
       $new_id = $this->unique_id('1');

       $style_id = $this->cmbTemplateType->getItemIndex();
       $result_style = mysql_query("SELECT value FROM lookup_vehicle_tbl WHERE id='".$style_id."'") or die("ERROR STYLE ".mysql_error());
       $row_style = mysql_fetch_array($result_style);
       $style_name = $row_style['value'];

       if($style_id=='All')
       {
        $style_name = 'All';
       }

       $result_template = mysql_query("insert into template_tbl (company_id,
       template_num,
       template_type,
       style_id,
       comments)

       values
       ('".$_SESSION['company_id']."',
       '".$new_id."',
       '".$style_name."',
       '".$style_id."',
       '".$this->memoComments->Text."')")or die("INSERT TEMPLATE-".mysql_error());

       echo"<script language=javascript>
       var answer = confirm('Add another record?');
       if(answer)
       {
       window.location = 'template_create.php';
       }
       else
       {
       window.location = 'template_list.php';
       }
       </script>";

       //redirect('template_list.php?template_id='.$this->hiddenTempId->getValue());
       }

}

global $application;

global $Task_Template;

//Creates the form
$Task_Template=new Task_Template($application);

//Read from resource file
$Task_Template->loadResource(__FILE__);

//Shows the form
$Task_Template->show();

?>